#include "GY_SignalGenerator.h"
#include <QDebug>

GY_SignalGenerator::GY_SignalGenerator(const QString &guid, QGraphicsItem *parent)
    :GY_BasicsCalculators(guid,this,parent)
{
    //图元类型
    GY_Object::setUserData(GY_ENUM::ClassType,QVariant("SignalGenerator"));
    initializationPropertyInfo();
    m_pTimer = new QTimer(this);
    connect(m_pTimer, &QTimer::timeout, [=]{
        if(m_PropertyInfoMap["OutputSignal"].propertyValue.toDouble() <= m_PropertyInfoMap["Value1"].propertyValue.toDouble()) {
            peakValue = false;
        }else if(m_PropertyInfoMap["OutputSignal"].propertyValue.toDouble() >= m_PropertyInfoMap["Value2"].propertyValue.toDouble()) {
            peakValue = true;
        }
        if(peakValue) {
            m_PropertyInfoMap["OutputSignal"].propertyValue = m_PropertyInfoMap["OutputSignal"].propertyValue.toDouble()-1;
        }else {
            m_PropertyInfoMap["OutputSignal"].propertyValue = m_PropertyInfoMap["OutputSignal"].propertyValue.toDouble()+1;
        }
//        qDebug()<<m_PropertyInfoMap["OutputSignal"].propertyValue.toDouble();
    });
}

GY_SignalGenerator::~GY_SignalGenerator()
{

}

void GY_SignalGenerator::initializationPropertyInfo()
{
    GY_PropertyInfo ObjectName(this);
    ObjectName.propertyName = "ObjectName";
    ObjectName.propertyValue = QString();
    ObjectName.updateType = Update;
    ObjectName.propertyType = QStringPropertyType;
    ObjectName.updatePropertyFun = (GY_PropertyInfo::UpdatePropertyFun)&GY_SignalGenerator::updateObjectName;
    m_PropertyInfoMap.insert(ObjectName.propertyName,ObjectName);
    m_PropertyInfoMap[ObjectName.propertyName].setConnect();

    GY_PropertyInfo OutputSignal(this);
    OutputSignal.propertyName = "OutputSignal";
    OutputSignal.propertyValue = 0;
    OutputSignal.updateType = Update;
    OutputSignal.propertyType = DoublePropertyType;
    OutputSignal.updatePropertyFun = (GY_PropertyInfo::UpdatePropertyFun)&GY_SignalGenerator::updateOutputSignal;
    m_PropertyInfoMap.insert(OutputSignal.propertyName,OutputSignal);
    m_PropertyInfoMap[OutputSignal.propertyName].setConnect();

    GY_PropertyInfo Value1(this);
    Value1.propertyName = "Value1";
    Value1.propertyValue = -1;
    Value1.updateType = Update;
    Value1.propertyType = DoublePropertyType;
    Value1.updatePropertyFun = (GY_PropertyInfo::UpdatePropertyFun)&GY_SignalGenerator::updateValue1;
    m_PropertyInfoMap.insert(Value1.propertyName,Value1);
    m_PropertyInfoMap[Value1.propertyName].setConnect();

    GY_PropertyInfo Value2(this);
    Value2.propertyName = "Value2";
    Value2.propertyValue = 1;
    Value2.updateType = Update;
    Value2.propertyType = DoublePropertyType;
    Value2.updatePropertyFun = (GY_PropertyInfo::UpdatePropertyFun)&GY_SignalGenerator::updateValue2;
    m_PropertyInfoMap.insert(Value2.propertyName,Value2);
    m_PropertyInfoMap[Value2.propertyName].setConnect();

    GY_PropertyInfo Frequency_Hz(this);
    Frequency_Hz.propertyName = "Frequency_Hz";
    Frequency_Hz.propertyValue = 1;
    Frequency_Hz.updateType = Update;
    Frequency_Hz.propertyType = DoublePropertyType;
    Frequency_Hz.updatePropertyFun = (GY_PropertyInfo::UpdatePropertyFun)&GY_SignalGenerator::updateFrequency_Hz;
    m_PropertyInfoMap.insert(Frequency_Hz.propertyName,Frequency_Hz);
    m_PropertyInfoMap[Frequency_Hz.propertyName].setConnect();

    GY_PropertyInfo DutyCycle_Percent(this);
    DutyCycle_Percent.propertyName = "DutyCycle_Percent";
    DutyCycle_Percent.propertyValue = 50;
    DutyCycle_Percent.updateType = Update;
    DutyCycle_Percent.propertyType = DoublePropertyType;
    DutyCycle_Percent.updatePropertyFun = (GY_PropertyInfo::UpdatePropertyFun)&GY_SignalGenerator::updateDutyCycle_Percent;
    m_PropertyInfoMap.insert(DutyCycle_Percent.propertyName,DutyCycle_Percent);
    m_PropertyInfoMap[DutyCycle_Percent.propertyName].setConnect();

    GY_PropertyInfo StartingPhase_Percent(this);
    StartingPhase_Percent.propertyName = "StartingPhase_Percent";
    StartingPhase_Percent.propertyValue = 0;
    StartingPhase_Percent.updateType = Update;
    StartingPhase_Percent.propertyType = DoublePropertyType;
    StartingPhase_Percent.updatePropertyFun = (GY_PropertyInfo::UpdatePropertyFun)&GY_SignalGenerator::updateStartingPhase_Percent;
    m_PropertyInfoMap.insert(StartingPhase_Percent.propertyName,StartingPhase_Percent);
    m_PropertyInfoMap[StartingPhase_Percent.propertyName].setConnect();

    GY_PropertyInfo UpdateRate_Hz(this);
    UpdateRate_Hz.propertyName = "UpdateRate_Hz";
    UpdateRate_Hz.propertyValue = 20;
    UpdateRate_Hz.updateType = Update;
    UpdateRate_Hz.propertyType = DoublePropertyType;
    UpdateRate_Hz.updatePropertyFun = (GY_PropertyInfo::UpdatePropertyFun)&GY_SignalGenerator::updateUpdateRate_Hz;
    m_PropertyInfoMap.insert(UpdateRate_Hz.propertyName,UpdateRate_Hz);
    m_PropertyInfoMap[UpdateRate_Hz.propertyName].setConnect();

    GY_PropertyInfo DurationLimit_mSecs(this);
    DurationLimit_mSecs.propertyName = "DurationLimit_mSecs";
    DurationLimit_mSecs.propertyValue = 0;
    DurationLimit_mSecs.updateType = Update;
    DurationLimit_mSecs.propertyType = IntPropertyType;
    DurationLimit_mSecs.updatePropertyFun = (GY_PropertyInfo::UpdatePropertyFun)&GY_SignalGenerator::updateDurationLimit_mSecs;
    m_PropertyInfoMap.insert(DurationLimit_mSecs.propertyName,DurationLimit_mSecs);
    m_PropertyInfoMap[DurationLimit_mSecs.propertyName].setConnect();

    GY_PropertyInfo SignalType(this);
    SignalType.propertyName = "SignalType";
    SignalType.propertyValue = "TRIANGULAR";//SQUARE,RANDOM,RAMP
    SignalType.updateType = Update;
    SignalType.propertyType = QStringPropertyType;
    SignalType.updatePropertyFun = (GY_PropertyInfo::UpdatePropertyFun)&GY_SignalGenerator::updateSignalType;
    m_PropertyInfoMap.insert(SignalType.propertyName,SignalType);
    m_PropertyInfoMap[SignalType.propertyName].setConnect();

    GY_PropertyInfo Enabled(this);
    Enabled.propertyName = "Enabled";
    Enabled.propertyValue = true;
    Enabled.updateType = Update;
    Enabled.propertyType = BoolPropertyType;
    Enabled.updatePropertyFun = (GY_PropertyInfo::UpdatePropertyFun)&GY_SignalGenerator::updateEnabled;
    m_PropertyInfoMap.insert(Enabled.propertyName,Enabled);
    m_PropertyInfoMap[Enabled.propertyName].setConnect();

    GY_PropertyInfo EnsureEveryPeriodExecutes(this);
    EnsureEveryPeriodExecutes.propertyName = "EnsureEveryPeriodExecutes";
    EnsureEveryPeriodExecutes.propertyValue = false;
    EnsureEveryPeriodExecutes.updateType = Update;
    EnsureEveryPeriodExecutes.propertyType = BoolPropertyType;
    EnsureEveryPeriodExecutes.updatePropertyFun = (GY_PropertyInfo::UpdatePropertyFun)&GY_SignalGenerator::updateEnsureEveryPeriodExecutes;
    m_PropertyInfoMap.insert(EnsureEveryPeriodExecutes.propertyName,EnsureEveryPeriodExecutes);
    m_PropertyInfoMap[EnsureEveryPeriodExecutes.propertyName].setConnect();
}

bool GY_SignalGenerator::updateOutputSignal(const QString &, const QVariant &)
{
    return false;
}

bool GY_SignalGenerator::updateValue1(const QString &name, const QVariant &value)
{
    if(!value.canConvert<double>()) {
        return false;
    }
    m_PropertyInfoMap[name].propertyValue = value;
    m_PropertyInfoMap["OutputSignal"].propertyValue = value.toDouble();
    return true;
}

bool GY_SignalGenerator::updateValue2(const QString &name, const QVariant &value)
{
    if(!value.canConvert<double>()) {
        return false;
    }
    m_PropertyInfoMap[name].propertyValue = value;
    return true;
}

bool GY_SignalGenerator::updateFrequency_Hz(const QString &name, const QVariant &value)
{
    if(!value.canConvert<double>()) {
        return false;
    }
    m_PropertyInfoMap[name].propertyValue = value;
    return true;
}

bool GY_SignalGenerator::updateDutyCycle_Percent(const QString &name, const QVariant &value)
{
    if(!value.canConvert<double>()) {
        return false;
    }
    m_PropertyInfoMap[name].propertyValue = value;
    return true;
}

bool GY_SignalGenerator::updateStartingPhase_Percent(const QString &name, const QVariant &value)
{
    if(!value.canConvert<double>()) {
        return false;
    }
    m_PropertyInfoMap[name].propertyValue = value;
    return true;
}

bool GY_SignalGenerator::updateUpdateRate_Hz(const QString &name, const QVariant &value)
{
    if(!value.canConvert<double>()) {
        return false;
    }
    m_PropertyInfoMap[name].propertyValue = value;
    return true;
}

bool GY_SignalGenerator::updateDurationLimit_mSecs(const QString &name, const QVariant &value)
{
    if(!value.canConvert<int>()) {
        return false;
    }
    m_PropertyInfoMap[name].propertyValue = value;
    return true;
}

bool GY_SignalGenerator::updateSignalType(const QString &name, const QVariant &value)
{
    if(!value.canConvert<QString>()) {
        return false;
    }
    m_PropertyInfoMap[name].propertyValue = value;
    return true;
}

bool GY_SignalGenerator::updateEnabled(const QString &name, const QVariant &value)
{
    if(!value.canConvert<bool>()) {
        return false;
    }
    m_PropertyInfoMap[name].propertyValue = value;
    return true;
}

bool GY_SignalGenerator::updateEnsureEveryPeriodExecutes(const QString &name, const QVariant &value)
{
    if(!value.canConvert<bool>()) {
        return false;
    }
    m_PropertyInfoMap[name].propertyValue = value;
    return true;
}

double GY_SignalGenerator::evOutputSignal()
{

}

void GY_SignalGenerator::evCompleted()
{

}

void GY_SignalGenerator::doStart()
{
    //Frequency_Hz * (value2-value1)为一跳,暂时取start(1)
    m_pTimer->start(1);
    m_PropertyInfoMap["Enabled"].propertyValue = true;
}

void GY_SignalGenerator::doStop()
{
    m_pTimer->stop();
    m_PropertyInfoMap["Enabled"].propertyValue = false;
}

void GY_SignalGenerator::doReset()
{
    m_PropertyInfoMap["OutputSignal"].propertyValue = m_PropertyInfoMap["Value1"].propertyValue.toDouble();
}


